<?php

namespace cms\modules\v1\models\system;

use cms\modules\v1\models\admin\Admin;
use cms\modules\v1\models\Common;
use common\models\Functions;
use yii\base\Exception;

/**
 * Class OperationLog
 * @package cms\modules\v1\models\system
 */
class OperationLog extends Common
{
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%admin_operate_log}}';
    }

    /**
     * 操作列表
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getList($page = 1, $size = 10) {
        try {
            $count = static::getCountByWhere([]);
            $offset = ($page - 1) * $size;
            $data = $this->setPageinationAPI($page, $size, $count);

            $order = 'id DESC';
            $list = static::getByWhere([], "*", $order, $offset, $size);
//            if(empty($list)) {
//                throw new Exception('未获取到内容');
//            }

            $adminMap = Admin::getListMapByCache();

            $result = [];
            foreach ($list as $k => $v) {
                $tmp['id'] = (int) $v['id'];
                $tmp['operate_user'] = $adminMap[$v['operate_admin_id']]['login_name'];
                $tmp['operate_nickname'] = $adminMap[$v['operate_admin_id']]['nick_name'];
                $tmp['where'] = json_decode($v['operate_condition'], true);
                $tmp['content'] = json_decode($v['operate_content'], true);
                $tmp['time'] = date("y/m/d H:d:s", strtotime($v['create_time']));
                $tmp['operate_type'] = $v['operate_type'];
                $tmp['mark'] = $v['mark'];

                $result[] = $tmp;
            }
            $data['data'] = $result;

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
}
